package com.netease.xone.itemview;

import media.MediaPlayerWrapper;
import media.MediaPlayerWrapper.MediaListener;
import Utils.StackTraceUtil;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.netease.log.NTLog;
import com.netease.xone.R;
import com.netease.xone.fragment.FragmentAudioBase;


public class RenderAudioPlayView implements OnClickListener, MediaListener {
    public static final String TAG = FragmentAudioBase.class.getSimpleName();
    
    private View mRoot = null;
    private TextView mName = null;
    private ImageView mPower = null;
    private ImageView mOperate = null;
    
    private OnClickListener mOnClickListener = null;

    public RenderAudioPlayView(View v) {
        if (null == v) {
            return;
        }
        mRoot = v;
        mName = (TextView)v.findViewById(R.id.name);
        mPower = (ImageView)v.findViewById(R.id.power);
        mOperate = (ImageView)v.findViewById(R.id.operate);
        
        mPower.setOnClickListener(this);
        mOperate.setOnClickListener(this);
        MediaPlayerWrapper.getInstance().registerMediaListener(this);
    }

    public void renderView(Context context) {
        String value = null;
        if (!TextUtils.isEmpty(MediaPlayerWrapper.getInstance().getName())) {
            value = context.getResources().getString(R.string.audio_play_name, MediaPlayerWrapper.getInstance().getName());
            mName.setText(value);
        }
        
        if (MediaPlayerWrapper.PlayStatus.PLAYING == MediaPlayerWrapper.getInstance().getPlayStatus()) {
            mOperate.setImageResource(R.drawable.icon_audio_pause);
        } else {
            mOperate.setImageResource(R.drawable.icon_audio_play);
        }
    }

    public void setOnClickListener(OnClickListener l) {
        mOnClickListener = l;
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.power:
                if (null != mOnClickListener) {
                    mOnClickListener.onClick(v);
                }
                MediaPlayerWrapper.getInstance().release();
                break;
            case R.id.operate:
                if (MediaPlayerWrapper.PlayStatus.PLAYING == MediaPlayerWrapper.getInstance().getPlayStatus()) {
                    MediaPlayerWrapper.getInstance().pause();
                    mOperate.setImageResource(R.drawable.icon_audio_play);
                } else {
                    MediaPlayerWrapper.getInstance().play();
                    mOperate.setImageResource(R.drawable.icon_audio_pause);
                }
                break;
        }
    }

    @Override
    public void onMediaPlay() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        mOperate.setImageResource(R.drawable.icon_audio_pause);
    }

    @Override
    public void onMediaPause() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        mOperate.setImageResource(R.drawable.icon_audio_play);
    }

    @Override
    public void onMediaRelease() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
        MediaPlayerWrapper.getInstance().removeMediaListener(this);
        mOnClickListener = null;
    }

    @Override
    public void onMediaCompletion() {
        NTLog.d(TAG, StackTraceUtil.getMethodName());
    }
}
